package com.zcyh.exam.parser;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.zcyh.exam.constants.EnumConstants;
import com.zcyh.exam.parser.base.AbstractExcelQuestionParser;
import com.zcyh.exam.parser.base.DefaultQuestionParser;
import com.zcyh.exam.parser.base.QuestionImportParser;
import com.zcyh.exam.utils.NumberUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 试题解析管理器
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/12/31 9:52
 */
public class QuestionImportParserManager {

    private QuestionImportParser excelQuestionParser;

    private static final QuestionImportParserManager me = new QuestionImportParserManager();

    public static QuestionImportParserManager build() {
        return me;
    }

    public void setExcelQuestionParser(QuestionImportParser excelQuestionParser) {
        this.excelQuestionParser = excelQuestionParser;
    }

    public QuestionImportParser getExcelQuestionParser() {
        return excelQuestionParser;
    }

    private QuestionImportParserManager() {

    }

    public QuestionImportParser createExcelQuestionParser(EnumConstants.QuestionType questionType) {
        if (this.getExcelQuestionParser() != null) {
            return getExcelQuestionParser();
        }

        if (questionType == EnumConstants.QuestionType.SINGLE_QUESTION
           || questionType == EnumConstants.QuestionType.MULTIPLE_QUESTION) {
            return new SelectQuestionParser();
        } else if (questionType == EnumConstants.QuestionType.JUDGMENT_QUESTION) {
            return new JudgmentQuestionParser();
        }
//        else if (questionType == EnumConstants.QuestionType.FILL_QUESTION) {
//            return new ExerciseSubjectiveQuestionParser();
//        }
        return new DefaultQuestionParser();
    }

    /**
     * excel 填空题解析器
     * @author zengjintao
     * @version 1.0
     * @create_at 2020/12/31 9:44
     */
    class ExerciseSubjectiveQuestionParser extends DefaultQuestionParser {

        @Override
        public String parseAnswerText(String answer) {
            if (ObjectUtil.isNotEmpty(answer)) {
                String[] answerArray = super.parserToken(answer);
                return JSONUtil.toJsonStr((answerArray));
            }
            return null;
        }
    }
    /**
     * 判断题解析器
     * @author zengjintao
     * @version 1.0
     * @create_at 2020/12/31 9:51
     */
    class JudgmentQuestionParser extends AbstractExcelQuestionParser {

        private static final int RIGHT = 1;
        private static final int ERROR = 0;

        @Override
        public String parseAnswerText(String answer) {
            if ("对".equals(answer)) {
                return String.valueOf(RIGHT);
            }
            return String.valueOf(ERROR);
        }
    }

    /**
     * 选择题解析器
     * @author zengjintao
     * @version 1.0
     * @create_at 2020/12/31 9:51
     */
    class SelectQuestionParser extends AbstractExcelQuestionParser {

   /* optionList: [
      {"label": "A", "option_name": "<p>说的</p>"},
      {"label": "B", "option_name": "<p>读书的</p>"},
      {"label": "C", "option_name": "<p>ere</p>"}
    ] */
        @Override
        public String parseOptionText(String option) {
            if (ObjectUtil.isEmpty(option)) {
                return option;
            }
            String[] optionArray = super.parserToken(option);
            if (optionArray == null) {
                return super.parseOptionText(option);
            }
            List<Map> optionList = new ArrayList<>();
            for (int i = 0; i < optionArray.length; i++) {
                Map data = new LinkedHashMap<>();
                String letter = NumberUtils.generateLetter(i);
                data.put("label", letter);
                data.put("option_name", "<p>" + optionArray[i] + "</p>");
                optionList.add(data);
            }
            return JSONUtil.toJsonStr(optionList);
        }
    }

}

